package person.bing.fbcloud.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mengfanbing
 * @date: 2020/8/5 8:51
 */
public class Q19 {
    public static void main(String[] args) {

    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        List<Integer> list = new ArrayList<>();
        while (true){
            if (head != null){
                list.add(head.val);
                head = head.next;
            }else {
                break;
            }
        }
        int size = list.size();
        if (size < n){
            return null;
        }
        list.remove(size-n);
        size = list.size();
        if (size > 0){
            ListNode ans = new ListNode(list.get(0));
            ListNode listNode = ans;
            for (int i=1;i<size;i++){
                listNode = listNode.next = new ListNode(list.get(i));
            }
            return ans;
        }
        return null;
    }

    public void createListNode(ListNode head,List<Integer> list,int size,int i){
        if (i<=size){
            head.next = new ListNode(list.get(i));
        }else {
            return;
        }
        createListNode(head.next,list,size,i++);
    }




     public class ListNode {
         int val;
         ListNode next;
         ListNode(int x) { val = x; }
     }
}
